package com.itsu.spbspider.service.impl;

import com.alibaba.fastjson.JSON;
import com.itsu.spbspider.exception.SystemException;
import ognl.Ognl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author suben
 * @create time 2020/2/12 18:17
 */
@Service
public class BaiduWeatherAreaParse {

    public Map parseObj(String content) throws Exception {
        if (StringUtils.isBlank(content)) {
            return null;
        }
        Map result = null;
        try {
            result = new HashMap();
            Map map = JSON.parseObject(content);
            List areaSourceList = (List) Ognl.getValue("data.area", map);
            List<Map> areaList = new ArrayList<>();
            for (Object area : areaSourceList) {
                Map areaMap = new HashMap();
                String id = BeanUtils.getProperty(area, "id");
                String name = BeanUtils.getProperty(area, "name");
                Boolean hasmore = Boolean.valueOf(BeanUtils.getProperty(area, "hasmore"));
                areaMap.put("id", id);
                areaMap.put("name", name);
                areaMap.put("hasmore", hasmore);
                areaList.add(areaMap);
            }
            result.put("data", areaList);
        } catch (Exception e) {
            throw new SystemException(e);
        }

        return result;
    }
}
